package com.yequan.ymdemo.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yequan.ymdemo.R;
import com.yequan.ymdemo.view.fragment.DoctorFragment;
import com.yequan.ymdemo.view.fragment.MineFragment;
import com.yequan.ymdemo.view.fragment.RijiFragment;
import com.yequan.ymdemo.view.fragment.ShequFragment;
import com.yequan.ymdemo.view.fragment.TaoZhengxingFragment;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private TextView mMainCity;
    private TextView mMainSearch;
    private ImageView mMainSaoYiSao;
    private ImageView mMainMessage;
    private RadioGroup mMainRg;
    private Fragment lastFragment;
    private TaoZhengxingFragment taoZhengxingFragment;
    private RijiFragment rijiFragment;
    private ShequFragment shequFragment;
    private DoctorFragment doctorFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        taoZhengxingFragment=new TaoZhengxingFragment();
        rijiFragment=new RijiFragment();
        shequFragment=new ShequFragment();
        doctorFragment=new DoctorFragment();
        mineFragment=new MineFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container,taoZhengxingFragment).commit();
        lastFragment=taoZhengxingFragment;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.taozhengxin:
                changeFragment(taoZhengxingFragment);
                break;
            case R.id.riji:
                changeFragment(rijiFragment);
                break;
            case R.id.shequ:
                changeFragment(shequFragment);
                break;
            case R.id.doctor:
                changeFragment(doctorFragment);
                break;
            case R.id.mine:
                changeFragment(mineFragment);
                break;
        }
    }

    private void initView() {
        mMainCity = (TextView) findViewById(R.id.main_city);
        mMainSearch = (TextView) findViewById(R.id.main_search);
        mMainSaoYiSao = (ImageView) findViewById(R.id.main_sao_yi_sao);
        mMainMessage = (ImageView) findViewById(R.id.main_message);
        mMainRg = (RadioGroup) findViewById(R.id.main_rg);
        mMainRg.setOnCheckedChangeListener(this);
    }

    private void changeFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            if (lastFragment != null) {
                fragmentTransaction.hide(lastFragment);
            }
            fragmentTransaction.add(R.id.main_container, fragment).commit();
        } else {
            fragmentTransaction.hide(lastFragment).show(fragment).commit();
        }
        lastFragment = fragment;
    }
}
